﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;
using Windows.Storage;


namespace WindowsPhone_Map_TTTN
{
    class MStorage
    {
        public MStorage()
        {
           
        }
        public static MStorage  getInstance()
        {
            if (_instatnce == null) _instatnce = new MStorage();
            return _instatnce;
        }

        private async Task WriteToFile()
        {
            // Get the text data from the textbox. 

            MGpx data = new MGpx();


            XDocument doc = new XDocument();

           
            // Get the local folder.
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

            // Create a new folder name DataFolder.
            var dataFolder = await local.CreateFolderAsync(MConfig.datafolder,
                CreationCollisionOption.OpenIfExists);

            // Create a new file named DataFile.txt.
            var file = await dataFolder.CreateFileAsync("log.pgx",
            CreationCollisionOption.OpenIfExists);

            // Write the data from the textbox.
            using (var s = await file.OpenStreamForWriteAsync())
            {
                doc.Save(s);
                s.Flush();
            }
        }
        private async Task ReadFile()
        {
            // Get the local folder.
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

            if (local != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await local.GetFolderAsync(MConfig.datafolder);

                // Get the file.
                var file = await dataFolder.OpenStreamForReadAsync("log.gpx");

                // Read the data.
                using (StreamReader streamReader = new StreamReader(file))
                {
                    String a = streamReader.ReadToEnd();
                }

            }
        }
        private static MStorage _instatnce;
    }
}
